package com.rrd.dw.mr.gzip.kafka;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;

import com.ucredit.babel.common.model.third.bangxinfen.BangXinFenData;
import com.ucredit.babel.common.model.third.bangxinfen.DhbMoboleStatusRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 电话邦-第一联系人在网状态
 *
 * @author renzhanzheng
 */

public class PhonehelpOnlinestatusParse extends ParseBase{
    private Text val = new Text();
    private Text key = new Text();
    private String subType = "result_203";

    /**
     * 电话邦-第一联系人在网状态
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {

        DhbMoboleStatusRecord obj = (DhbMoboleStatusRecord)model;
        BangXinFenData objdata = obj.getData();
        JSONObject info = objdata.getData() == null ? new JSONObject() : JSON.parseObject(objdata.getData());
        JSONObject data = info.getJSONObject("data") == null ? new JSONObject() : info.getJSONObject("data");
        JSONObject result = data.getJSONObject("result") == null ? new JSONObject() : data.getJSONObject("result");
        if (!result.containsKey(subType)){
            return false;
        }

        JSONObject result203 = result.getJSONObject(subType) == null ? new JSONObject() : result.getJSONObject(subType);
        JSONObject resultData = result203.getJSONObject("data") == null ? new JSONObject() : result203.getJSONObject("data");

        StringBuilder sb = getBaseInfo(obj, successful);
        //接口描述
        sb.append(result203.getString("msg") == null ? "" : result203.getString("msg")).append(CRT001);
        //在网状态
        sb.append(resultData.getString("rescode") == null ? "" : resultData.getString("rescode")).append(CRT001);
        //状态码,为0则收费
        sb.append(result203.getString("status") == null ? "" : result203.getString("status")).append(CRT001);
        //sid
        sb.append(result203.getString("sid") == null ? "" : result203.getString("sid")).append(CRT001);
        StringBuilder txSb = getBaseInfo(obj);
        key.set(getKey());
        val.set((sb.toString() + txSb.toString()).replace("\n", "").replace("\r", " "));
        context.write(key, val);
        return true;

    }

    @Override
    public String getSubType() {
        return "." + subType;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_phonehelp_onlinestatus_inc";
    }

    @Override
    public String getType() {
        return RecordType.DHB_MOBILE_STATUS_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "电话邦-第一联系人在网状态";
    }
}
